library(mice)
library(VIM)
library(lattice)
library(survival)
library(tidyverse)

str(nhanes)

# Find NA ----

# summary() function display the NA count
summary(nhanes)

# Calculate the NAs in a column
sum(is.na(nhanes$bmi))
mean(is.na(nhanes$bmi))  # The percentage of NAs

# Get the completed observation in the dataframe
sum(complete.cases(nhanes))
mean(!complete.cases(nhanes))  # The percentage of rows that has NA

# NA mode ----
md.pattern(nhanes)

fluxplot(nhanes)

aggr(nhanes, prop = FALSE, numbers = TRUE)

matrixplot(nhanes)

# Row del ----
new.nhanes <- nhanes[complete.cases(nhanes), ]

# How to choose a fill method ----
imp <- mice(nhanes, print = FALSE)
plot(imp)

stripplot(imp, chl ~ .imp, pch = 20, cex = 2)

stripplot(imp)

densityplot(imp)


imp <- mice(nhanes, seed = 123)
fit <- with(imp, lm())
complete(imp, action = 3)
head(nhanes)
